package com.jason.xposedjason;

import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

import java.lang.reflect.Field;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

/**
 * @author：Jason
 * @date：2021/10/29 14:32
 * @email：1129847330@qq.com
 * @description:
 */
public class Tutorial implements IXposedHookLoadPackage {
    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
        XposedBridge.log("load app：" + lpparam.packageName);

        if (lpparam.packageName.equals("com.jason.demo")) {
            XposedHelpers.findAndHookMethod(
                    "com.jason.demo.MainActivity",
                    lpparam.classLoader,
                    "onCreate",
                    Bundle.class,
                    new XC_MethodHook() {
                        @Override
                        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                            //不能通过Class.forName()来获取Class ，在跨应用时会失效
                            Class c = lpparam.classLoader.loadClass("com.jason.demo.MainActivity");
                            Field field = c.getDeclaredField("textView");
                            field.setAccessible(true);
                            //param.thisObject 为执行该方法的对象，在这里指MainActivity
                            TextView textView = (TextView) field.get(param.thisObject);
                            textView.setText("Hello Xposed,我厉害吧");
                            textView.setTextColor(Color.RED);
                        }
                    });
        }
    }
}